package cn.lyjuan.android.demofive.ui.slide.viewpage;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import cn.lyjuan.android.demofive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chad on 2016/3/22.
 */
public class ViewPagerIndexActivity extends FragmentActivity
{
    private List<View> pages;// 滑动页面
    private ViewGroup main;// 主布局
    private ViewGroup indexGroup;// 索引组
    private ViewPager pager;// 滑动页面控件
    private ImageView[] indexImgs;// 索引圆点图片集合
    private static final int SIZE = 2;
    private static final int HANDLER_WHAT_AUTO_SCROLL = 1;// 自动滚动标识

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = this.getLayoutInflater();

        // 添加页面
        pages = new ArrayList<View>(SIZE + 2);
        pages.add(inflater.inflate(R.layout.ui_tabhost3_second, null));
        pages.add(inflater.inflate(R.layout.ui_tabhost3_first, null));
        pages.add(inflater.inflate(R.layout.ui_tabhost3_second, null));
//        pages.add(inflater.inflate(R.layout.ui_tabhost3_third, null));
//        pages.add(inflater.inflate(R.layout.ui_tabhost3_fourth, null));
        pages.add(inflater.inflate(R.layout.ui_tabhost3_first, null));

        // 获取必要元素
        main = (ViewGroup) inflater.inflate(R.layout.ui_viewpager_index, null);
        indexGroup = (ViewGroup) main.findViewById(R.id.ui_viewpager_index_indexgroup);
        pager = (ViewPager) main.findViewById(R.id.ui_viewpager_pager);

        indexImgs = new ImageView[SIZE];

        ImageView index = null;
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(32, 32);
        for (int i = 0; i < SIZE; i++)
        {
            index = new ImageView(ViewPagerIndexActivity.this);
            index.setLayoutParams(layoutParams);
            index.setPadding(20, 0, 20, 0);
            index.setBackgroundResource(R.drawable.ui_viewpager_index_gray);

            indexImgs[i] = index;// 以后用于设置选中状态
            indexGroup.addView(index);// 设置到UI中
        }
        indexImgs[0].setBackgroundResource(R.drawable.ui_viewpager_index_light);// 默认第一张图片

        this.setContentView(main);

        PageAdapter adp = new PageAdapter();
        pager.setAdapter(adp);
        pager.addOnPageChangeListener(adp);
        Thread.currentThread().getStackTrace();


        pager.setCurrentItem(1);
    }

    class PageAdapter extends PagerAdapter
        implements ViewPager.OnPageChangeListener
    {
        @Override
        public int getCount()
        {
            return pages.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object o)
        {
            return view == o;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {
            container.removeView(pages.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            View v = pages.get(position);

            container.addView(v);

            Log.i(ViewPagerIndexActivity.class.getSimpleName(), "实例化page >> " + position);

            return v;
        }

        @Override
        public void onPageSelected(int position)
        {
            switch (position)
            {
                case 0 :
                    position = SIZE - 1;
                    break;
                case SIZE + 1 :
                    position = 0;
                    break;
                default:
                    position--;
            }

            // 将所有设置为未选中状态
            for (int i = 0; i < indexImgs.length; i++)
            {
                indexImgs[i].setBackgroundResource(R.drawable.ui_viewpager_index_gray);
            }
            // 设置选中状态
            indexImgs[position].setBackgroundResource(R.drawable.ui_viewpager_index_light);

            // 打开自动滑动
            delayScroll(position);
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
            if (ViewPager.SCROLL_STATE_DRAGGING == positionOffset)// 拖动开始
                clearScroll();// 取消自动滑动

            if (ViewPager.SCROLL_STATE_IDLE == positionOffset)// 拖动完成
            {
                Log.i(ViewPagerIndexActivity.class.getSimpleName(), "onPageScrolled p >> " + position);
                if (0 == position)
                {
                    pager.setCurrentItem(pages.size() - 2, false);// 跳转到实际上的最后一张
                    Log.w(ViewPagerIndexActivity.class.getSimpleName(), "onPageScrolled goto LAST >> p >> " + position);
                }
                else if (pages.size() - 1 == position)
                {
                    pager.setCurrentItem(1, false);// 跳转到实际上的第一张
                    Log.w(ViewPagerIndexActivity.class.getSimpleName(), "onPageScrolled goto FIRST >> p >> " + position);
                }
            }
        }

        @Override
        public void onPageScrollStateChanged(int state)
        {

            Log.w(ViewPagerIndexActivity.class.getSimpleName(), "onPageScrollStateChanged state >> " + state);
        }
    }



    private Handler autoScrollHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            int position = pager.getCurrentItem() + 1;
            position = position >= pages.size() ? 0 : position;
            pager.setCurrentItem(position);
        }
    };

    private void delayScroll(int position)
    {
        clearScroll();
        autoScrollHandler.sendEmptyMessageDelayed(HANDLER_WHAT_AUTO_SCROLL, 4000);
        Log.i(ViewPagerIndexActivity.class.getSimpleName(), "打开自动滑动 p >> " + position);

    }

    private void clearScroll()
    {
        Log.i(ViewPagerIndexActivity.class.getSimpleName(), "关闭自动滑动");
        autoScrollHandler.removeMessages(HANDLER_WHAT_AUTO_SCROLL);
    }

    @Override
    protected void onRestart()
    {
        delayScroll(-1);
        super.onResume();
    }

    @Override
    protected void onStop()
    {
        clearScroll();
        super.onStop();
    }
}
